package com.ktjiaoyu.service.impl;

import com.ktjiaoyu.entity.SysUser;
import com.ktjiaoyu.mapper.SysUserMapper;
import com.ktjiaoyu.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Service("sysUserServiceImpl")
public class SysUserServiceImpl implements SysUserService {
    //定义Mapper
    @Autowired
    private SysUserMapper sysUserMapper;


    @Override
    public List<SysUser> getSysUserList(SysUser sysUser) {
        return sysUserMapper.getSysUserList(sysUser);
    }

    @Transactional
    @Override
    public int savenBatch(List<SysUser> sysUserList) {
        int rows=0;
        for (int i = 0; i < sysUserList.size(); i++) {
            //拿出每一条用户数据插入
            SysUser sysUser=sysUserList.get(i);
            if (i==1){
                //插入第二条数据时 模拟数据库异常
                throw new RuntimeException("模拟数据库异常");
            }
            rows+=sysUserMapper.insert(sysUser);
        }
        return rows;
    }

    @Override
    public SysUser login(String account, String password) {
        SysUser loginUser=sysUserMapper.getByAccount( account);
        //如果账号存在且密码相等 则返回查到的登录用户信息
        if (loginUser!=null && loginUser.getPassword().equals(password)){
            return loginUser;
        }
        return null;
    }
}